using UnityEngine;
using System.Collections;

public class ViewDelimiter : MonoBehaviour {

	#region Attributes
	
	public float xCollisionDistance = 0.0f;
	public float zCollisionDistance = 0.0f;
	
	[HideInInspector]
	public Vector3 newVPPosition;
	
	#endregion
	
	#region Methods
	
	private void UpdateViewPoint()
	{
		
		float xPosition;
		float zPosition;
        /* Check LimitTrigger Hit */
        int layerMask = TagConstants.LAYER_LIMIT_TRIGGER;
		
		RaycastHit hit;
        if (Physics.Raycast(this.transform.position, Vector3.right, out hit, this.xCollisionDistance, layerMask)
			&& hit.collider.tag == TagConstants.TAG_LIMIT_TRIGGER)
		{
			xPosition = hit.point.x - xCollisionDistance;
			this.newVPPosition = new Vector3(xPosition,
				this.transform.position.y,
				this.transform.position.z);
		}		
		else if (Physics.Raycast(this.transform.position, Vector3.left, out hit, this.xCollisionDistance, layerMask)
			&& hit.collider.tag == TagConstants.TAG_LIMIT_TRIGGER)
		{
			xPosition = hit.point.x + xCollisionDistance;
			this.newVPPosition = new Vector3(xPosition,
				this.transform.position.y,
				this.transform.position.z);
		}
		else{
			this.newVPPosition = this.transform.position;
		}
		
		
		if (Physics.Raycast(this.transform.position, Vector3.forward, out hit, this.zCollisionDistance, layerMask)
			&& hit.collider.tag == TagConstants.TAG_LIMIT_TRIGGER)
		{
			zPosition = hit.point.z - zCollisionDistance;
			this.newVPPosition = new Vector3(this.newVPPosition.x,
				this.transform.position.y,
				zPosition);
		}
		else if (Physics.Raycast(this.transform.position, Vector3.back, out hit, this.zCollisionDistance, layerMask)
			&& hit.collider.tag == TagConstants.TAG_LIMIT_TRIGGER)
		{
			zPosition = hit.point.z + zCollisionDistance;
			this.newVPPosition = new Vector3(this.newVPPosition.x,
				this.transform.position.y,
				zPosition);
		}
		else{
			this.newVPPosition = new Vector3(this.newVPPosition.x,
				this.transform.position.y,
				this.transform.position.z);
		}
        
	}
	
	#endregion
	
	#region Unity Methods

	void Start () {
		
		this.newVPPosition = this.transform.position;
	}
	
	// Update is called once per frame
	void Update () {
		
		UpdateViewPoint();
	
	}
	#endregion
}
